#ifndef _HTTPSESSION_H
#define _HTTPSESSION_H

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <hippolib/net/httpheader.hpp>
#include "../common/logmanager.h"

class HTTPSession
{
public:
	HTTPSession(boost::shared_ptr<boost::asio::ip::tcp::socket> clientsock,
					boost::shared_ptr<boost::asio::ip::tcp::socket> serversock);
	~HTTPSession();

	void Initialize(const std::string & method, const std::string & protocol, const std::string & host, const std::string & path,
			const std::string & version, std::map<std::string, hippolib::httpheader, hippolib::HeaderKeyComparer> headers,
			const std::string & hostname, bool is_via_proxy, boost::asio::streambuf & remaining_buffer,
			boost::system::error_code & error);
	static unsigned long getSessionCounter();
private:
	void terminate();
	void handle_server_write(const boost::system::error_code & error, size_t bytes_transferred);
	void handle_client_write(const boost::system::error_code & error, size_t bytes_transferred);
	void handle_server_read(const boost::system::error_code & error, size_t bytes_transferred);
	void handle_client_read(const boost::system::error_code & error, size_t bytes_transferred);

	static unsigned long m_session_counter;
	
	boost::shared_ptr<boost::asio::ip::tcp::socket> m_clientsock;
	boost::shared_ptr<boost::asio::ip::tcp::socket> m_serversock;
	
	char *m_client_readbuffer;
	char *m_server_readbuffer;

	log4cxx::LoggerPtr m_logger;

	// not intialized in constructor
	boost::asio::ip::tcp::endpoint m_client_endpoint;
	boost::asio::ip::tcp::endpoint m_server_endpoint;

	bool m_server_pending_read;
	bool m_server_pending_write;
	bool m_client_pending_read;
	bool m_client_pending_write;
	bool m_terminating;
	boost::mutex m_term_mutex;
};

#endif
